package net.m2technologies.open_arm.transaction;

import org.opengroup.arm40.transaction.ArmIdentityProperties;
import org.opengroup.arm40.transaction.ArmTransactionFactory;

import java.util.Collections;
import java.util.Map;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
public class ArmIdentityPropertiesImpl extends ArmInterfaceImpl implements ArmIdentityProperties {

    private final Map identityProperties;
    private final Map contextProperties;

    public ArmIdentityPropertiesImpl(final Map identityProperties,
                                     final Map contextProperties,
                                     final ArmTransactionFactory creatingFactory) {
        super(creatingFactory);
        this.identityProperties = identityProperties;
        this.contextProperties = contextProperties;
    }

    public ArmIdentityPropertiesImpl(final ArmIdentityPropertiesImpl toBeCopied) {
        super(toBeCopied.getCreatingFactory());
        this.identityProperties = toBeCopied.getIdentityProperties();
        this.contextProperties = toBeCopied.getContextProperties();
    }

    public String getIdentityName(final int index) {
        return ((IdentityProperty) this.identityProperties.get(new Integer(index))).getKey();
    }

    public String getIdentityValue(final int index) {
        return ((IdentityProperty) this.identityProperties.get(new Integer(index))).getValue();
    }

    public String getContextName(final int index) {
        return ((ContextProperty) this.contextProperties.get(new Integer(index))).getKey();
    }

    public String getContextValue(final int index) {
        return ((ContextProperty) this.contextProperties.get(new Integer(index))).getValue();
    }

    public String setContextValue(final int index, final String newValue) {
        final ContextProperty property = (ContextProperty) this.contextProperties.get(new Integer(index));
        final String result = property.setValue(newValue);
        this.contextProperties.put(new Integer(index), property);
        return result;
    }

    public void setContextValues(final String[] values) {
        for (int i = 0; i < values.length; i++) {
            setContextValue(i, values[i]);
        }
    }

    private Map getIdentityProperties() {
        return Collections.unmodifiableMap(identityProperties);
    }

    private Map getContextProperties() {
        return Collections.unmodifiableMap(contextProperties);
    }

    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof ArmIdentityPropertiesImpl)) return false;

        final ArmIdentityPropertiesImpl armIdentityProperties = (ArmIdentityPropertiesImpl) o;

        return null != contextProperties
               ? !contextProperties.equals(armIdentityProperties.contextProperties)
               : null == armIdentityProperties.contextProperties
                 && !(null != identityProperties
                      ? !identityProperties.equals(armIdentityProperties.identityProperties)
                      : null != armIdentityProperties.identityProperties);

    }

    public int hashCode() {
        int result;
        result = null != identityProperties ? identityProperties.hashCode() : 0;
        result = HASH_CODE_MAGIC_NUMBER * result + (null != contextProperties ? contextProperties.hashCode() : 0);
        return result;
    }

}
